package flower.com.fenzu35011111.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import flower.com.fenzu35011111.bean.Groups;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对消息数据库的CRUD的操作
 */
public class Z_Groups_Dao {
    private Context context;
    private Dao<Groups, Integer> userDao;
    private Z_DataBaseHelper helper;

    public Z_Groups_Dao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(Groups.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addGroups(String name


    ) {
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
            Groups life_msg = new Groups();
            life_msg.setId(String.valueOf(Math.random() * 10));
            life_msg.setTime(df.format(new Date()));
            life_msg.setName(name);
            userDao.create(life_msg);

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("88888", "" + e);
        }
    }

    List<Groups> XTWUserInfos;

    public void query() {
        try {
            List<Groups> XTWUserInfoss = userDao.queryBuilder().where().eq("name", "自选1").query();
            if (XTWUserInfoss.size() == 0) {
                addGroups("全部");
                addGroups("自选1");
                addGroups("自选2");
            } else {
                Log.e("1111", "xx");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public List<Groups> ischunzai(String name) {
        try {
            List<Groups> XTWUserInfoss = userDao.queryBuilder().where().eq("name", name).query();
            return XTWUserInfoss;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public List<Groups> queryall() {
        try {
            XTWUserInfos = userDao.queryForAll();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return XTWUserInfos;
    }

    public void deleteUser(Groups user) {
        try {
            userDao.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
